package com.example.wo.httpapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button btnStart;
    private TextView tvResult;
    private Handler handler=new Handler(){
    public  void handleMessage(Message msg){
        String text = (String) msg.obj;
        tvResult.setText(text);
        }
    };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult=findViewById(R.id.tv_result);
        editText = findViewById(R.id.edt);
        btnStart = findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String phone = editText.getText().toString();
                final String data = "mobileCode="+phone+"&userID=";
                final String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL u = new URL(url);
                            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
                            conn.setRequestMethod("POST");
                            conn.setReadTimeout(5000);
                            conn.setConnectTimeout(5000);
                            conn.setDoOutput(true);
                            OutputStream out = conn.getOutputStream();
                            out.write(data.getBytes());
                            conn.connect();
                            int responseCode = conn.getResponseCode();
                            if(responseCode==200){
                                InputStream is = conn.getInputStream();
                                String text = getStringFromInputStream(is);
                                Message msg = new Message();
                                msg.obj = text;
                                handler.sendMessage(msg);
                            }
                            out.close();
                            conn.disconnect();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    private String getStringFromInputStream(InputStream is) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        String text = null;
        try {
            while((text = br.readLine()) != null)
            {
                sb.append(text+"/r/n");
            }
            int start = text.indexOf("\">")+2;
            int end = text.indexOf("</");
            text = text.substring(start, end);
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return text;
    }
}
